import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
export const asyncRoutes=[
  {
    path: '/park',
    component: Layout,
    permission: 'park',
    meta: { title: '园区管理', icon: 'el-icon-office-building' },
    children: [{
      path: 'building',
      permission: 'park:building',
      meta: { title: '楼宇管理' },
      component: () => import('@/views/Park/Building/index')
    },
    {
      path: 'enterprise',
      permission: 'park:enterprise',
      meta: { title: '企业管理' },
      component: () => import('@/views/Park/Enterprise/index')
    }]
  },

  {
    path: '/parking',
    component: Layout,
    permission: 'parking',
    meta: { title: '行车管理', icon: 'el-icon-guide' },
    children: [ {
      path: 'card',
      permission: 'parking:card',
      component: () => import('@/views/Car/CarCard'),
      meta: { title: '月卡管理' }
    }, 
    {
      path: 'rule',
      permission: 'parking:rule',
      component: () => import('@/views/Car/CarRule'),
      meta: { title: '计费规则管理' }
    }]
  },
  {
    path: '/pole',
    component: Layout,
    permission: 'pole',
    meta: { title: '一体杆管理', icon: 'el-icon-refrigerator' },
    children: [{
      path: 'info',
      permission: 'pole:info',
      component: () => import('@/views/Rod/RodManage'),
      meta: { title: '一体杆管理' }
    }]
  },
  {
    path: '/sys',
    component: Layout,
    permission: 'sys',
    meta: { title: '系统管理', icon: 'el-icon-setting' },
    children: [{
      path: 'role',
      permission: 'sys:role',
      component: () => import('@/views/System/Role/index'),
      meta: { title: '角色管理' }
    }, {
      path: 'user',
      permission: 'sys:user',
      component: () => import('@/views/System/Employee/index'),
      meta: { title: '员工管理' }
    }]
  },
]
export const routes = [
  {
    path: '/login',
    component: () => import('@/views/Login/index'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/workbench'
  },
  {
    path: '/workbench',
    component: Layout,
    children: [{
      path: '',
      component: () => import('@/views/Workbench/index'),
      meta: { title: '工作台', icon: 'el-icon-data-board' }
    }]
  },
  {
    path: '/addenterprise',
    component: () => import('@/views/Park/Enterprise/addenterprise'),
    hidden: true
  },
  {
    path: '/carxq',
    component: () => import('@/views/Car/CarCard/detail'),
    hidden: true
  },
  {
    path: '/carxf',
    component: () => import('@/views/Car/CarCard/xf'),
    hidden: true
  },
  {
    path: '/addcar',
    component: () => import('@/views/Car/CarCard/add'),
    hidden: true
  },
  {
    path: '/addrole',
    component: () => import('@/views/System/Role/addrole'),
    hidden: true
  },
  {
    path: '/enterprisexq',
    component: () => import('@/views/Park/Enterprise/Enterprisexq'),
    hidden: true
  },
 
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  mode: 'history',
  scrollBehavior: () => ({ y: 0 }),
  routes: [...routes]
})

const router = createRouter()

// 重置路由方法
export function resetRouter() {
  // 得到一个全新的router实例对象
  const newRouter = createRouter()
  // 使用新的路由记录覆盖掉老的路由记录
  router.matcher = newRouter.matcher
}

function getFirstrouter(permarr){
  const newArr=permarr.map(item=>{
    return item.split(":")[0]
  })
  return [...new Set(newArr)]
}


function getSecondrouter(permarr){
  const newArr=permarr.map(item=>{
    const arr=item.split(":");
    return `${arr[0]}:${arr[1]}`
  })
  return [...new Set(newArr)]
}
function getRoutes(firstRouter,secondRouter,asyncRoutes){
  if(firstRouter.includes("*")){
    return asyncRoutes
  }
  return asyncRoutes.filter(route=>{
    return firstRouter.includes(route.permission)
  }).map(item=>{
    return{
      ...item,
      children:item.children.filter(item=>secondRouter.includes(item.permission))
    }
  })
}
import { getToken } from "../utils/auth";
import store from '@/store'
const WHITE_LIST=["/login","/404"]
console.log(router,"router");
router.beforeEach(async(to,from,next)=>{
    const token=getToken();
    if(token){
        next()

        if(!store.state.user.profile.id){
          const permision=await store.dispatch("user/getprofilelist")
          console.log(permision,"permission");
          const firstRouter=getFirstrouter(permision)
          console.log(firstRouter,"first");
          const secondRouter=getSecondrouter(permision)
          console.log(secondRouter,"second");
         const routes=getRoutes(firstRouter,secondRouter,asyncRoutes);
         console.log("最终路由表",routes);
         routes.forEach(route=>{
          router.addRoute(route)
         })
         store.commit("menu/setMenuList",routes)
        }
    }else{
        if(WHITE_LIST.includes(to.path)){
            next()
        }else{
            next("/login")
        }
    }

})

export default router
